package com.ruoyi.chat.service.impl;

import java.util.List;
import java.util.Optional;

import com.ruoyi.chat.domain.ChatContent;
import com.ruoyi.chat.mapper.ChatContentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.chat.mapper.ChatMapper;
import com.ruoyi.chat.domain.Chat;
import com.ruoyi.chat.service.IChatService;

/**
 * 聊天记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-18
 */
@Service
public class ChatServiceImpl implements IChatService 
{
    @Autowired
    private ChatMapper chatMapper;

    @Autowired
    private ChatContentMapper contentMapper;

    /**
     * 查询聊天记录
     * 
     * @param id 聊天记录主键
     * @return 聊天记录
     */
    @Override
    public Chat selectChatById(Long id)
    {
        Chat chat = chatMapper.selectChatById(id);
        Optional<ChatContent> content = contentMapper.findById(id);
        ChatContent chatContent = content.orElse(new ChatContent());
        chat.setContent(chatContent.getContent());
        return chat;
    }

    /**
     * 查询聊天记录列表
     * 
     * @param chat 聊天记录
     * @return 聊天记录
     */
    @Override
    public List<Chat> selectChatList(Chat chat)
    {
        List<Chat> chats = chatMapper.selectChatList(chat);
        for (Chat chat1 : chats) {
            Long id = chat1.getId();
            Optional<ChatContent> chatContent = contentMapper.findById(id);
            ChatContent chatContent1 = chatContent.orElse(new ChatContent());
            chat1.setContent(chatContent1.getContent());
        }
        return chats;
    }

    /**
     * 新增聊天记录
     * 
     * @param chat 聊天记录
     * @return 结果
     */
    @Override
    public int insertChat(Chat chat)
    {
        int i = chatMapper.insertChat(chat);
        if (i>0){
            ChatContent chatContent = new ChatContent();
            chatContent.setContent(chat.getContent());
            chatContent.setId(chat.getId());
            contentMapper.insert(chatContent);
        }
        return i;
    }

    /**
     * 修改聊天记录
     * 
     * @param chat 聊天记录
     * @return 结果
     */
    @Override
    public int updateChat(Chat chat)
    {
        int i = chatMapper.updateChat(chat);
        if (i>0){
            ChatContent chatContent = new ChatContent();
            chatContent.setContent(chat.getContent());
            chatContent.setId(chat.getId());
            contentMapper.save(chatContent);
        }
        return i;
    }

    /**
     * 批量删除聊天记录
     * 
     * @param ids 需要删除的聊天记录主键
     * @return 结果
     */
    @Override
    public int deleteChatByIds(Long[] ids)
    {
        int i = chatMapper.deleteChatByIds(ids);
        if (i>0){
            for (Long id : ids) {
                contentMapper.deleteById(id);
            }
        }
        return i;
    }

    /**
     * 删除聊天记录信息
     * 
     * @param id 聊天记录主键
     * @return 结果
     */
    @Override
    public int deleteChatById(Long id)
    {
        int i = chatMapper.deleteChatById(id);
        if (i>0){
            contentMapper.deleteById(id);
        }
        return i;
    }
}
